<template>
  <div>
    <el-dialog
      title="新增目录"
      :visible.sync="dialogVisible"
      width="25%"
      :before-close="handleClose"
    >
      <span>
        <el-form
          :model="ruleForm"
          :rules="ruless"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <!--  -->
          <template>
            <el-form-item label="所属学科" prop="subjectID">
              <el-select
                class="log"
                v-model="ruleForm.subjectID"
                placeholder="请选则所属学科"
              >
                <el-option
                  v-for="item in tableData"
                  :key="item.id"
                  :label="item.subjectName"
                  :value="item.subjectID"
                ></el-option>
              </el-select>
            </el-form-item>
          </template>
          <!--  -->
          <el-form-item label="目录名称" prop="directoryName">
            <el-input v-model="ruleForm.directoryName"></el-input>
          </el-form-item>
          <!--  -->
        </el-form>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { add, update } from '@/api/hmmm/directorys'
export default {
  props: {
    tableData: {
      type: Array
    }
  },
  data () {
    return {
      dialogVisible: false,
      ruleForm: {
        directoryName: '',
        subjectID: '',
        id: null
      },
      ruless: {
        directoryName: [
          { required: true, message: '请选择目录名称', trigger: 'blur' }
        ],
        subjectID: [
          { required: true, message: '请选择所属学科', trigger: 'blur' }
        ]
      },
      isall: false
    }
  },
  methods: {
    handleClose (done) {
      this.$confirm('确认关闭？')
        .then((_) => {
          done()
        })
        .catch((_) => {})
    },
    // 添加
    confirm () {
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          if (this.isall === false) {
            const res = await add({
              directoryName: this.ruleForm.directoryName,
              subjectID: this.ruleForm.subjectID
            })
            this.dialogVisible = false
            this.ruleForm.directoryName = ''
            this.ruleForm.subjectID = ''
            if (res.status === 200) {
              this.$emit('Therefresh')
            }
            this.dialogVisible = false
            // alert('submit!')
          } else {
            const res = await update(this.ruleForm)
            this.dialogVisible = false
            if (res.status === 200) {
              this.$emit('Therefresh')
            }
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep.log {
  width: 17vw !important;
}
</style>
